<?php
namespace app\sysadmin\controller;
use think\Db;
class Bannerimages extends Common{
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		$_input_data = input('get.');
		$_get_key = array('id','cat_id','title','status','push');
		$_get_str = "";
		
		if($_input_data){
			foreach($_input_data as $k=>$v){
				$_v = trim($v);
				if(in_array($k,$_get_key)){
					if($_v != ''){
						$_get_str .= $k.":'".strval($_v)."',";
					}else{
						$_input_data[$k] = '';
					}
				}else{
					unset($_input_data[$k]);
				}
			}
		}
		if($_get_key){
			foreach($_get_key as $a){
				if(!isset($_input_data[$a])){
					$_input_data[$a] = '';
				}
			}
		}
		$this->assign('get_data',$_input_data);
		$this->assign('get_str',$_get_str);
		$this->assign('poster_lists',$this->get_poster_lists());
		return $this->fetch();
	}
	public function add(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');
			
			$_data = array();
			$_data['cat_id'] = (int)$_post_data['cat_id'];
			$_data['title'] = $_post_data['title'];
			$_data['url'] = $_post_data['url'];
			$_data['img'] = $_post_data['img'];
			$_data['status'] = $_post_data['status'];
			$_data['start_time'] = empty($_post_data['start_time']) ? 0 : strtotime($_post_data['start_time']);
			$_data['end_time'] = empty($_post_data['end_time']) ? 0 : strtotime($_post_data['end_time']);
			$_data['sort'] = (int)$_post_data['sort'];
			$_data['info'] = $_post_data['info'];
			$_data['onclick_num'] = 0;
			$_data['create_time'] = time();
			$_data['update_time'] = time();
			
			$ads_date_fields = $this->check_ads_data_fields($_data);
			if(!$ads_date_fields['code']){
				$this->error($ads_date_fields['msg']);exit;
			}
			
			$_ret_id = Db::name('ads')->insertGetId($_data);
			if($_ret_id){
				$this->add_operation_log($_ret_id,$_data);
				$this->success("添加成功",url('index'));
			}else{
				$this->error("添加失败");
			}
			exit;
		}
		
		$_info = array(
			'id'=>'0',
			'cat_id'=>'0',
			'title'=>'',
			'url'=>'',
			'img'=>'',
			'status'=>'1',
			'start_date'=>'',
			'end_date'=>'',
			'sort'=>'0',
			'info'=>'',
		);
		$this->assign('info',$_info);
		$this->assign('poster_lists',$this->get_poster_lists());
		$this->assign('page_name','添加广告');
		return $this->fetch('info');
	}
	public function edit(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');
			
			$_id = (int)$_post_data['id'];
			
			$_data = array();
			$_data['cat_id'] = (int)$_post_data['cat_id'];
			$_data['title'] = $_post_data['title'];
			$_data['url'] = $_post_data['url'];
			$_data['img'] = $_post_data['img'];
			$_data['status'] = $_post_data['status'];
			$_data['start_time'] = empty($_post_data['start_time']) ? 0 : strtotime($_post_data['start_time']);
			$_data['end_time'] = empty($_post_data['end_time']) ? 0 : strtotime($_post_data['end_time']);
			$_data['sort'] = (int)$_post_data['sort'];
			$_data['info'] = $_post_data['info'];
			$_data['update_time'] = time();
			
			$ads_date_fields = $this->check_ads_data_fields($_data);
			if(!$ads_date_fields['code']){
				$this->error($ads_date_fields['msg']);exit;
			}
			
			$_ret_id = Db::name('ads')->where('id','=',$_id)->update($_data);
			if($_ret_id){
				$this->add_operation_log($_id,$_data);
				$this->success("编辑成功",url('index'));
			}else{
				$this->error("编辑失败");
			}
			exit;
		}
		$_id = (int)(input('get.id'));
		$_info = Db::name('ads')->where('id','=',$_id)->find();
		if(!$_info){
			return $this->tips_404();exit;
		}
		$_info['start_date'] = $_info['start_time']?date('Y-m-d H:i:s',$_info['start_time']):'';
		$_info['end_date'] = $_info['end_time']?date('Y-m-d H:i:s',$_info['end_time']):'';
		
		$this->assign('info',$_info);
		$this->assign('poster_lists',$this->get_poster_lists());
		$this->assign('page_name','编辑广告');
		return $this->fetch('info');
	}
	public function del(){
		if($this->request->isGet() == true){
			$_id = (int)input('get.id');
			if(!$_id){
				$this->error("请选择需要删除的信息！");
			}
			$_info = Db::name('ads')->where('id','=',$_id)->find();
			if(!$_info){
				$this->error("请选择需要删除的信息！");
			}
			$_ret = Db::name('ads')->where('id','=',$_id)->delete();
			if($_ret){
				$this->add_operation_log($_id,$_info);
				$this->success("删除成功",url('index'));
			}else{
				$this->error("删除失败，请刷新以后重试！");
			}
			
		}elseif($this->request->isPost() == true){
			$_ids = input('post.id');
			if(!$_ids){
				$this->error("请选择需要删除的信息！");
			}
			$_info = Db::name('ads')->where('id','in',$_ids)->select();
			if(!$_info){
				$this->error("请选择需要删除的信息！");
			}
			$_ret = Db::name('ads')->where('id','in',$_ids)->delete();
			if($_ret){
				$this->add_operation_log($_ids,$_info);
				$this->success("批量删除成功",url('index'));
			}else{
				$this->error("批量删除失败，请刷新以后重试！");
			}
			
		}else{
			$this->error("系统错误！");
		}
	}
	
	// 异步查询广告列表
	public function ajax_lists(){
		if($this->request->isAjax() != true){
			$this->error('404');
		}
		$_input_data = input();
		$page = $_input_data['page'] >= 1 ? (int)$_input_data['page'] : '0';
		$limit = $_input_data['limit'] >= 1 ? (int)$_input_data['limit'] : '15';
		unset($_input_data['page']);
		unset($_input_data['limit']);
		$_where = array();
		if(!empty($_input_data['id'])){
			$_input_data['id'] = (int)$_input_data['id'];
			$_where[] = array('id','=',$_input_data['id']);
		}
		if(!empty($_input_data['title'])){
			$_where[] = array('title','like','%'.$_input_data['title'].'%');
		}
		
		if(!empty($_input_data['cat_id'])){
			$_input_data['cat_id'] = (int)$_input_data['cat_id'];
			$_where[] = array('cat_id','=',$_input_data['cat_id']);
		}
		
		if(!empty($_input_data['push'])){
			$_push_time = explode('~',$_input_data['push']);
			$_where[] = array('create_time','between',[strtotime($_push_time[0]),strtotime($_push_time[1])]);
		}
		if(!empty($_input_data['status'])){
			$_time = time();
			switch($_input_data['status']){
				case 1:
					$_where[] = array('status','exp',Db::raw(' = 1 and (`start_time` = 0 AND `end_time` = 0) or 
						(`start_time` = 0 AND `end_time` > '.$_time.') or 
						(`start_time` < '.$_time.' AND `end_time` = 0) or 
						(`start_time` < '.$_time.' AND `end_time` > '.$_time.')'));
		
					break; 
				case 2:
					$_where[] = array('status','=','1');
					$_where[] = array('start_time','>=',$_time);
					
					break; 
				case 3:
					$_where[] = array('status','=','1');
					$_where[] = array('end_time','<=',$_time);
					
					break; 
				case 4:
					$_where[] = array('status','=','0');
					
					break; 
				default:
					// break; 
			}
		}
		
		$count = Db::name('ads')->where($_where)->count();
		$lists = Db::name('ads')->where($_where)->page($page.','.$limit)->field('id,cat_id,title,url,img,status,start_time,end_time,sort,onclick_num,create_time')
					->order('cat_id asc,status desc,sort desc,id desc')->select();
		if($lists){
			$poster_lists = $this->get_poster_lists();
			$poster_lists = ArrayKey($poster_lists);
			foreach($lists as $k=>$v){
				$lists[$k]['cat_name'] = $poster_lists[$v['cat_id']]['name'];
				$lists[$k]['create_date'] = date('Y-m-d H:i:s',$v['create_time']);
				
				$lists[$k]['start_date'] = $v['start_time']?date('Y-m-d H:i:s',$v['start_time']):'--未设置--';
				$lists[$k]['end_date'] = $v['end_time']?date('Y-m-d H:i:s',$v['end_time']):'--未设置--';
				
				$lists[$k]['start_time'] = $v['start_time'] ? $v['start_time'] : 0;
				$lists[$k]['end_time'] = $v['end_time'] ? $v['end_time'] : 0;
			}
		}
		$_json = array();
		$_json['code'] = 0;
		$_json['msg'] = 'success';
		$_json['count'] = $count;
		$_json['data'] = $lists;
		echo json_encode($_json);
		exit;
	}
	
	// 查询广告位
	private function get_poster_lists(){
		$_lists = cache('sysadmin_ads_poster_lists');
		if($_lists){
			return $_lists;
		}
		$_lists = Db::name('ads_cat')->field('id,name,info')->order('id asc')->select();
		cache('sysadmin_ads_poster_lists',$_lists,3600);
		return $_lists;
	}
	
	// 验证广告字段
	private function check_ads_data_fields($_data){
		// 验证广告id cat_id
		$poster_lists = $this->get_poster_lists();
		$_cat_id_all = array();
		if($poster_lists){
			foreach($poster_lists as $k=>$v){
				$_cat_id_all[] = $v['id'];
			}
		}
		if(!in_array($_data['cat_id'],$_cat_id_all)){
			return array('code'=>'0','msg'=>'请选择有效的广告位');exit;
		}
		// 验证广告名称 title
		if(empty($_data['title'])){
			return array('code'=>'0','msg'=>'请输入广告名称');exit;
		}
		// 验证广告图片地址 img
		if(empty($_data['img'])){
			return array('code'=>'0','msg'=>'请输入广告图片地址');exit;
		}
		return array('code'=>'1','msg'=>'验证通过');
	}
}
?>